import type { Menu } from "@/types/modules/system";
import type { AppRouteRecordRaw } from "@/router/types";
import { regexUrl } from "@/utils/index";

const layouts = import.meta.glob('/src/layout/*.vue')
const views = import.meta.glob('/src/views/**/index.vue')
const modules = {
  ...layouts,
  ...views
}

const _buildVueRouter = (menu: Menu): AppRouteRecordRaw => {
  const { name, path, component, children, redirect, meta } = menu
  // @ts-ignore
  const { title, cache, hidden, order, icon, affix } = meta
  return {
    redirect,
    name,
    path,
    component: modules[`/src${component}`],
    children: children?.filter(route => !regexUrl.test(route.path))?.map(_buildVueRouter),
    meta: {
      title,
      icon,
      cache,
      hidden,
      order,
      affix
    }
  }
}
// eslint-disable-next-line import/prefer-default-export
export const buildVueRouter  = (menus: Menu[]): AppRouteRecordRaw[] => menus.filter(menu => !regexUrl.test(menu.path)).map(_buildVueRouter)




